<?php
/**
 * Created by PhpStorm.
 * User: baidu
 * Date: 17/8/5
 * Time: 下午4:37
 */
namespace app\api\controller\v1;

use app\api\controller\Common;
use think\Controller;
use app\common\lib\exception\ApiException;
// use app\common\lib\Aes;

class Upvote extends AuthBase {

  /**
   * [save 点赞功能开发]
   * @return [type] [description]
   */
  public function save(){
    $id = input('post.id',0,'intval');
    if (empty($id)) {
      return show(config('code.error'),'id inexistence',[],404);
    }

    //判断这个id的新闻文章是否存在 -->ent_news
    $newsData = model('News')->get(['id'=>$id]);
    if (empty($newsData) || $newsData['status'] != 1) {
       return show(config('code.error'),'news error ',[],404);
    }

    $data = [
      'user_id'=>$this->user->id,
      'news_id'=>$id,
    ];
    //查询库中是否存在
    $userNews = model('UserNews')->get($data);
    if (!empty($userNews)) {
      return show(config('code.error'),'Have some great',[],404);
    }

    try {
      $userNewsId = model('UserNews1')->add($data);
      if ($userNewsId) {
        model('News')->where(['id' => $id])->setInc('upvote_count');
        return show(config('code.success'),'success',[],200);
      }else{
        return show(config('code.error'),'Have some great',[],404);
      }
    } catch (\Exception $e) {
      return show(config('code.error'),'内部error',[],404);
    }
    halt($newsData);
    return [];
  }


  /**
   * [delete 取消点赞]
   * @return [type] [description]
   */
  public function delete(){
    $id = input('delete.id',0,'intval');
    if (empty($id)) {
      return show(config('code.error'),'id inexistence',[],404);
    }
     $data = [
      'user_id'=>$this->user->id,
      'news_id'=>$id,
    ];
    //查询库中是否存在
    $userNews = model('UserNews')->get($data);
    if (empty($userNews)) {
      return show(config('code.error'),'您没点赞无法取消',[],404);
    }

    try {
      $userNewsId = model('UserNews')
                    ->where($data)
                    ->delete();
      if ($userNewsId) {
          model('News')->where(['id' => $id])->setDec('upvote_count');
          return show(config('code.success'),'success',[],200);
        }else{
          return show(config('code.error'),'Have some great',[],404);
        }
    } catch (\Exception $e) {
      return show(config('code.error'),'内部error',[],404);
    }
  }

  /**
   * 查看文章是否被该用户点赞
   */
  public function read(){
      $id = input('param.id',0,'intval');
      if (empty($id)) {
        return show(config('code.error'),'id inexistence',[],404);
      }

      //判定这个id的新闻文章->ent_news 小伙伴自行加入
      //查询库里边是否存在 点赞
       $data = [
        'user_id'=>$this->user->id,
        'news_id'=>$id,
      ];
      //查询库中是否存在
      $userNews = model('UserNews')->get($data);
      if (empty($userNews)) {
        return show(config('code.success'),'ok',['isUpvote'=>0],200);
      }else{
        return show(config('code.success'),'ok',['isUpvote'=>1],200);
      }
  }

    public function read1(){
      throw new ApiHandleException("Error Processing Request", 1);
      
  }






}